package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.workflow.model.ActDefModel;
import com.ccp.dev.workflow.model.BpmProStatus;
import com.ccp.dev.workflow.service.ActDefModelService;
import com.ccp.dev.workflow.service.BpmProStatusService;
import com.ccp.dev.workflow.service.BpmnImageService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.RepositoryService;
import org.flowable.ui.common.service.exception.InternalServerErrorException;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ClassDescribe:
 * 流程图生成控制器
 * @author :wangcheng
 * Date: 2019-06-05
 */
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmnImage")
@Controller
public class BpmnImageController extends SysBaseController {

    @Resource
    private BpmnImageService bpmnImageService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private ActDefModelService actDefModelService;
    @Resource
    private BpmProStatusService bpmProStatusService;


    /**
     * 流程图显示页面
     * @return 流程图页面
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/diagram")
    @Action(description = "流程图显示页面", detail = "跳转流程图显示页面")
    public String viewDiagram(@RequestParam String modelId, Model model) {
        model.addAttribute("modelId", modelId);
        return "workflow/actDefModel/bpmnImage";
    }

    /**
     * 获取生成流程图所需的json数据
     * @param request http请求
     */
    @RequestMapping(value = "/modelJson", method = RequestMethod.GET, produces = "application/json")
    public void getModelImageJson(HttpServletResponse response, HttpServletRequest request) {
        String processInstanceId= RequestUtil.getString(request,"procInstId");
        String procDefId= RequestUtil.getString(request,"procDefId");
        //获取流程节点状态
        List<BpmProStatus> list= bpmProStatusService.queryByProcInstanceId(processInstanceId);
        BpmnModel bpmnModel;
        bpmnModel = repositoryService.getBpmnModel(procDefId);
        if (bpmnModel == null || bpmnModel.getLocationMap().isEmpty()) {
            throw new InternalServerErrorException("Process definition could not be found ");
        }
        ObjectNode displayNode = bpmnImageService.processProcessElements(bpmnModel,list);
        this.print(response,displayNode);
    }

    /**
     * 获取静态流程图json数据
     * @param modelId 模型id
     * @param response http响应
     */
    @RequestMapping(value = "json/getByModelId", method = RequestMethod.GET)
    public void getByModelId(@RequestParam String modelId,HttpServletResponse response) {
        ActDefModel model = actDefModelService.getOneById(modelId);
        BpmnModel bpmnModel = repositoryService.getBpmnModel(model.getProcDefId());
        JsonNode jsonNode=  bpmnImageService.processProcessElements(bpmnModel,null);
        this.print(response,jsonNode);
    }

    /**
     * 输出json到输出流
     * @param response http响应
     * @param jsonNode 流程图json数据
     */
    private void print(HttpServletResponse response,JsonNode jsonNode){
        try {
            response.setHeader("Content-type",MediaType.APPLICATION_JSON_UTF8_VALUE);
            response.setCharacterEncoding("UTF-8");
            response.getWriter().print(jsonNode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
